/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.system.RuntimeInfo;
import cn.hutool.system.SystemUtil;
import vip.dexian.admin.base.BaseController;
import vip.dexian.admin.dto.AdminPrincipal;
import vip.dexian.admin.service.AdminService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ServletContextAware;

/**
 * Main controller
 *
 * @author 挺好的 2023年05月31日 10:27
 */
@Controller ("adminMainController")
@RequestMapping ("/admin/main")
public class MainController extends BaseController implements ServletContextAware {

    /**
     * 管理员
     */
    @Resource (name = "adminServiceImpl")
    private AdminService adminService;

    /**
     * 进入主页面
     *
     * @param model
     *         {@link org.springframework.ui.ModelMap}
     *
     * @return /admin/main/main
     */
    @GetMapping
    public String main (ModelMap model) {

        model.addAttribute("javaVersion", System.getProperty("java.version"));
        model.addAttribute("javaHome", System.getProperty("java.home"));
        model.addAttribute("osName", System.getProperty("os.name"));
        model.addAttribute("osArch", System.getProperty("os.arch"));
        model.addAttribute("serverInfo", this.servletContext.getServerInfo());
        model.addAttribute("servletVersion",
                this.servletContext.getMajorVersion() + "." + this.servletContext.getMinorVersion()
        );

        // 运行时内存
        RuntimeInfo runtimeInfo = SystemUtil.getRuntimeInfo();
        model.addAttribute("maxMemory", FileUtil.readableFileSize(runtimeInfo.getMaxMemory()));
        model.addAttribute("totalMemory", FileUtil.readableFileSize(runtimeInfo.getTotalMemory()));
        model.addAttribute("freeMemory", FileUtil.readableFileSize(runtimeInfo.getFreeMemory()));
        model.addAttribute("usableMemory", FileUtil.readableFileSize(runtimeInfo.getUsableMemory()));

        AdminPrincipal admin = this.adminService.getCurrent();

        model.addAttribute("admin", admin);

        return "/admin/main/main";
    }

    /**
     * 图标icon
     *
     * @param modelMap
     *
     * @return
     */
    @GetMapping ("/icon")
    public String icon (ModelMap modelMap) {
        return "/admin/main/icon";
    }


    private ServletContext servletContext;

    @Override
    public void setServletContext (ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
